package com.taorenqi.business.fragment.login;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taorenqi.business.R;
import com.taorenqi.business.activity.LoginActivity;
import com.taorenqi.business.app.MyApp;
import com.taorenqi.business.event.StartFragmentEvent;
import com.taorenqi.business.fragment.base.BaseBackFragment;
import com.taorenqi.business.utils.Encoder;
import com.taorenqi.business.utils.ToastUtil;
import com.taorenqi.business.widget.statusview.StatusView;
import com.taorenqi.business.widget.subscribers.PageDataSubscriberOnNext;
import com.taorenqi.business.widget.subscribers.ProgressSubscriber;
import com.taorenqi.data.common.BasePageableResponse;
import com.taorenqi.data.entity.login.LoginEntity;
import com.taorenqi.data.service.LoginDataHttpRequest;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 企业用户完善资料
 * A simple {@link Fragment} subclass.
 */
public class CompleteCompanyInfoFragment extends BaseBackFragment {


    @Bind(R.id.status_view)
    StatusView statusView;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.et_improve_company_name)
    EditText etImproveCompanyName;
    @Bind(R.id.tv_improve_company_sex)
    TextView tvImproveCompanySex;
    @Bind(R.id.et_improve_company_alibaba)
    EditText etImproveCompanyAlibaba;
    @Bind(R.id.et_improve_company_qq)
    EditText etImproveCompanyQq;
    @Bind(R.id.et_improve_company_wechat)
    EditText etImproveCompanyWechat;
    @Bind(R.id.et_improve_company_compname)
    EditText etImproveCompanyCompname;
    @Bind(R.id.et_improve_company_phone)
    EditText etImproveCompanyPhone;
    @Bind(R.id.et_improve_company_email)
    EditText etImproveCompanyEmail;
    @Bind(R.id.tv_improve_company_bind)
    TextView tvImproveCompanyBind;
    @Bind(R.id.tv_improve_company_waittodo)
    TextView tvImproveCompanyWaittodo;
    @Bind(R.id.tv_improve_company_login)
    TextView tvImproveCompanyLogin;
    @Bind(R.id.activity_improve_company_info)
    LinearLayout activityImproveCompanyInfo;

    private Dialog dialog;
    private int sex = 0;

    private BasePageableResponse<LoginEntity> data;
    private PageDataSubscriberOnNext<BasePageableResponse<LoginEntity>> subscriber;

    public static CompleteCompanyInfoFragment newInstance() {
        // Required empty public constructor
        CompleteCompanyInfoFragment fragment = new CompleteCompanyInfoFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_complete_company_info, container, false);
        ButterKnife.bind(this, view);
        initToolbar();
        initSex(sex);
        initData();
        return attachToSwipeBack(view);
    }

    private void initData() {

        subscriber = new PageDataSubscriberOnNext<BasePageableResponse<LoginEntity>>() {
            @Override
            public void onNext(BasePageableResponse<LoginEntity> entity) {
                data = entity;
                start(BindingShopFragment.newInstance());
            }
        };
    }

    private void initToolbar() {
        toolbar.setTitle("完善资料");
        initToolbarNav(toolbar);
    }

    private void initSex(int sex) {
        switch (sex) {
            case 0:
                tvImproveCompanySex.setText("请选择您的性别");
                tvImproveCompanySex.setTextColor(getResources().getColor(R.color.myGrayText));
                break;
            case 1:
                tvImproveCompanySex.setText(" 男 ");
                tvImproveCompanySex.setTextColor(getResources().getColor(R.color.register_input_coplor));
                break;
            case 2:
                tvImproveCompanySex.setText(" 女 ");
                tvImproveCompanySex.setTextColor(getResources().getColor(R.color.register_input_coplor));
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_improve_company_sex,R.id.tv_improve_company_bind, R.id.tv_improve_company_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_improve_company_sex:
                showdialog();
                break;
            case R.id.tv_improve_company_bind:
                String name = etImproveCompanyName.getText().toString().trim();
                String wangwang = etImproveCompanyAlibaba.getText().toString().trim();
                String weixin = etImproveCompanyWechat.getText().toString().trim();
                String qq = etImproveCompanyQq.getText().toString().trim();
                String gender = "";
                if (sex == 0){
                    ToastUtil.showToast(_mActivity,"请选择性别");
                } else {
                    gender = sex + "";
                }
                String companyname = etImproveCompanyCompname.getText().toString().trim();
                String companyphone = etImproveCompanyPhone.getText().toString().trim();
                String companyemail = etImproveCompanyEmail.getText().toString().trim();

                HashMap map = new HashMap();
                map.put("shop_truename", name);
                map.put("shop_sex", gender);
                map.put("shop_wangwang", wangwang);
                map.put("shop_weixinopenid", weixin);
                map.put("shop_qq", qq);
                map.put("enterprise_name", companyname);
                map.put("enterprise_tel", companyphone);
                map.put("shop_enterprise_email", companyemail);
                String sign = Encoder.getSigns(map);

                LoginDataHttpRequest.getInstance(MyApp.getApp()).postCompleteInfo(new ProgressSubscriber(subscriber, _mActivity), name,gender, wangwang,
                        weixin,qq,companyname,companyphone,companyemail,sign);
                break;
            case R.id.tv_improve_company_login:
                Intent intent = new Intent();
                intent.setClass(_mActivity, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
        }
    }


    private void showdialog() {
        dialog = new Dialog(_mActivity, R.style.my_dialog);
        dialog.setCanceledOnTouchOutside(true);
        LinearLayout view = (LinearLayout) LayoutInflater.from(_mActivity).inflate(R.layout.bottom_dialog, null);
        view.findViewById(R.id.btn_man).setOnClickListener(btnlistener);
        view.findViewById(R.id.btn_woman).setOnClickListener(btnlistener);
        view.findViewById(R.id.btn_cancel).setOnClickListener(btnlistener);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
//        dialogWindow.setWindowAnimations(R.style.dialogstyle);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        lp.x = 0; // 新位置X坐标
        lp.y = -20; // 新位置Y坐标
        //lp.y = getWindowManager().getDefaultDisplay().getHeight();; // 新位置Y坐标
        //lp.width = (int) getResources().getDisplayMetrics().widthPixels; // 宽度
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT; // 宽度
        view.measure(0, 0);
        //lp.height = view.getMeasuredHeight();
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        lp.alpha = 9f; // 透明度
        //dialogWindow.setAttributes(lp);
        dialogWindow.setAttributes(lp);
        dialog.show();
    }

    private View.OnClickListener btnlistener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_man:
                    sex = 1;
                    initSex(sex);
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                    break;

                case R.id.btn_woman:
                    sex = 2;
                    initSex(sex);
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                    break;
                // 取消
                case R.id.btn_cancel:
                    if (dialog != null) {
                        dialog.dismiss();
                        sex = 0;
                        initSex(sex);
                    }
                    break;
            }
        }
    };
}
